package com.mycompany.project.server;

import java.util.ArrayList;
import java.util.Iterator;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import marketplace.componente.carrocompras.CarroComprasRemote;
import marketplace.objects.TransaccionBO;

import com.mycompany.project.client.CarroComprasService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class CarroComprasServiceImpl extends RemoteServiceServlet implements CarroComprasService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void agregarAlCarrito(TransaccionBO elementoTransaccion) {
		// TODO Auto-generated method stub
		Context ctx;
		try {
			ctx = new InitialContext();
			//NamingEnumeration<NameClassPair> stuff = ctx.list("");
			//while(stuff.hasMore()){
			//NameClassPair na = stuff.next();
			//System.out.println(na.getClassName()+" "+na.getName());
			//}
			CarroComprasRemote carro = (CarroComprasRemote) ctx.lookup("java:global/EnterpriseMarketplaceProject/ComponenteCarroCompras/CarroCompras");
			carro.agregarAlCarrito(elementoTransaccion);
			
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			System.out.println("aqui fue");
			e.printStackTrace();
			
		}
	}

	@Override
	public void eliminarDelCarrito(TransaccionBO servicioAEliminar) {
		// TODO Auto-generated method stub
		Context ctx;
		try {
			ctx = new InitialContext();
			//NamingEnumeration<NameClassPair> stuff = ctx.list("");
			//while(stuff.hasMore()){
			//NameClassPair na = stuff.next();
			//System.out.println(na.getClassName()+" "+na.getName());
			//}
			CarroComprasRemote carro = (CarroComprasRemote) ctx.lookup("java:global/EnterpriseMarketplaceProject/ComponenteCarroCompras/CarroCompras");
			carro.eliminarDelCarrito(servicioAEliminar);
			
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			System.out.println("aqui fue");
			e.printStackTrace();
			
		}
		
	}

	@Override
	public ArrayList<TransaccionBO> darElementosDelCarrito() {
		// TODO Auto-generated method stub
		Context ctx;
		try {
			ctx = new InitialContext();
			//NamingEnumeration<NameClassPair> stuff = ctx.list("");
			//while(stuff.hasMore()){
			//NameClassPair na = stuff.next();
			//System.out.println(na.getClassName()+" "+na.getName());
			//}
			CarroComprasRemote carro = (CarroComprasRemote) ctx.lookup("java:global/EnterpriseMarketplaceProject/ComponenteCarroCompras/CarroCompras");
			return carro.darElementosDelCarrito();
			
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			System.out.println("aqui fue");
			e.printStackTrace();
			
		}
		return null;
	}

	@Override
	public double darValorTotalDelCarrito() {
		// TODO Auto-generated method stub
		Context ctx;
		try {
			ctx = new InitialContext();
			//NamingEnumeration<NameClassPair> stuff = ctx.list("");
			//while(stuff.hasMore()){
			//NameClassPair na = stuff.next();
			//System.out.println(na.getClassName()+" "+na.getName());
			//}
			CarroComprasRemote carro = (CarroComprasRemote) ctx.lookup("java:global/EnterpriseMarketplaceProject/ComponenteCarroCompras/CarroCompras");
			return carro.darValorTotalDelCarrito();
			
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			System.out.println("aqui fue");
			e.printStackTrace();
			
		}
		return 0;
	}

	@Override
	public void efectuarCompra() {
		// TODO Auto-generated method stub
		Context ctx;
		try {
			ctx = new InitialContext();
			//NamingEnumeration<NameClassPair> stuff = ctx.list("");
			//while(stuff.hasMore()){
			//NameClassPair na = stuff.next();
			//System.out.println(na.getClassName()+" "+na.getName());
			//}
			CarroComprasRemote carro = (CarroComprasRemote) ctx.lookup("java:global/EnterpriseMarketplaceProject/ComponenteCarroCompras/CarroCompras");
			carro.efectuarCompra();
			
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			System.out.println("aqui fue");
			e.printStackTrace();
			
		}
		
	}
}
